const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const AutoImport = require('unplugin-auto-import/webpack').default
const Components = require('unplugin-vue-components/webpack').default
const {NaiveUiResolver} = require('unplugin-vue-components/resolvers')
const packageName = "DToolsFinderIQ"; // 定义项目打包后文件名

// 复制文件夹到指定目录
const copyFiles = [
    {
        from: path.resolve("src/manifest.json"),
        to: `${path.resolve(packageName)}/manifest.json`
    },
    {
        from: path.resolve("public"),
        to: path.resolve(packageName)
    }
];

// 复制插件
const plugins = [
    new CopyWebpackPlugin({
        patterns: copyFiles
    }),
    new HtmlWebpackPlugin({
        filename: 'index.html', // 输出的 HTML 文件名
        template: 'src/newtab/newtab.html', // 使用的模板文件
        inject: 'body', // 将所有资源注入到 body 底部
        chunks: ['newtab']
    }),
    // 添加自动导入插件
    AutoImport({
        imports: [
            'vue',
            {
                'naive-ui': [
                    'useDialog',
                    'useMessage',
                    'useNotification',
                    'useLoadingBar'
                ]
            }
        ]
    }),
    Components({
        resolvers: [NaiveUiResolver()]
    })
];

module.exports = {
    pages: {
        popup: {
            entry: `src/popup/popup.js`,
            template: `src/popup/popup.html`,
            filename: `popup.html`
        },
        newtab: {
            entry: `src/newtab/newtab.js`,
            template: `src/newtab/newtab.html`,
            filename: `newtab.html`
        },
        fileExpress: {
            entry: `src/fileExpress/fileExpress.js`,
            template: `src/fileExpress/fileExpress.html`,
            filename: `fileExpress.html`
        }
    },
    productionSourceMap: false,
    outputDir: __dirname + '/' + packageName,
    configureWebpack: {
        watch: false,
        entry: {
            newtab: "./src/newtab/newtab.js",
            content: "./src/content-scripts/content-script.js",
            background: "./src/background/background.js",
            popup: "./src/popup/popup.js",
            fileExpress: "./src/fileExpress/fileExpress.js"
        },
        output: {
            filename: "js/[name].js"
        },
        plugins,
        optimization: {
            splitChunks: false
        }
    },
    css: {
        extract: {
            filename: "css/[name].css"
        }
    }
}